package com.lin.demo;

import com.lin.demo.service.CacheKeyGenerator;
import com.lin.demo.service.impl.CacheKeyGeneratorImp;
import com.lin.demo.service.task.AsyncTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@EnableRetry // 启用对 @Retryable 注解的扫描
@EnableScheduling
public class MainApplication implements CommandLineRunner {
    private final static Logger logger= LoggerFactory.getLogger(MainApplication.class);

    public static void main (String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(MainApplication.class, args);
        Environment env = application.getEnvironment();
        logger.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n\t"+
                        "Doc: \thttp://{}:{}/doc.html\n"+
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"));
    }

    @Bean
    public CacheKeyGenerator cacheKeyGenerator(){
        return new CacheKeyGeneratorImp();
    }

//    @Resource
//    private TestRetryService testRetryService;

    @Resource
    private AsyncTask asyncTask;

    @Override
    public void run(String... args) {
//        System.out.println(testRetryService.test(0));
//        asyncTask.doTask1();
//        asyncTask.doTask2();
    }
}
